var express = require('express');
var router = express.Router();

const alipaySdk = require('../alipay')
const AlipayFormData = require('alipay-sdk/lib/form').default

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

router.post('/api/payment',async (req,res,next) => {
  // console.log(typeof req.body);
  const { orderId,totalAmount } = req.body
  const formData = new AlipayFormData()
  formData.setMethod('get')
  formData.addField("returnUrl","http://baidu.com/")
  formData.addField('bizContent', {
    outTradeNo: orderId,
    productCode: 'FAST_INSTANT_TRADE_PAY',
    totalAmount: totalAmount,
    subject: '商品',
    body: '商品详情',
  });
  
  const result = await alipaySdk.exec(
    'alipay.trade.page.pay',
    {},
    { formData: formData },
  );
  res.send({
    msg:'success',
    url:result,
    code:200
  })
})

module.exports = router;
